<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">





  <link rel="alternate" href="/atom.xml" title="学习笔记" type="application/atom+xml">






<meta name="description" content="个人学习笔记,好记性不如烂笔头.">
<meta name="keywords" content="C++ MFC NodeJS 郑诗增笔记">
<meta property="og:type" content="website">
<meta property="og:title" content="学习笔记">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="学习笔记">
<meta property="og:description" content="个人学习笔记,好记性不如烂笔头.">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="学习笔记">
<meta name="twitter:description" content="个人学习笔记,好记性不如烂笔头.">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/">





  <title>学习笔记</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?21a4e5fb6baffd52827a2abc94849c6b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>
<a href="https://zhengshizeng.github.io" class="github-corner" aria-label="View source on GitHub">
<svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true">
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
<path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
<path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
</svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">学习笔记</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">郑诗增的学习笔记</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_14模板引擎ejs/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_14模板引擎ejs/" itemprop="url">14模板引擎ejs</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:14+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_14模板引擎ejs/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_14模板引擎ejs/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="十四、模板引擎ejs"><a href="#十四、模板引擎ejs" class="headerlink" title="十四、模板引擎ejs"></a>十四、模板引擎ejs</h3><figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="keyword">install</span> ejs</span><br></pre></td></tr></table></figure>
<h6 id="1-变量-lt-name-gt"><a href="#1-变量-lt-name-gt" class="headerlink" title="1.变量 &lt;%= name %&gt;"></a>1.变量 &lt;%= name %&gt;</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">%=json.arr[0].user%</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="2-for-循环"><a href="#2-for-循环" class="headerlink" title="2.for 循环"></a>2.for 循环</h6><figure class="highlight django"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span> <span class="attr">for</span>(<span class="attr">var</span> <span class="attr">i</span>=<span class="string">0;i</span>&lt;<span class="attr">json.arr.length</span>;<span class="attr">i</span>++)&#123; %&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>用户名：<span class="tag">&lt;<span class="name">%=json.arr[i].user%</span>&gt;</span> 密码：<span class="tag">&lt;<span class="name">%=json.arr[i].pass%</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span> &#125; %&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br></pre></td></tr></table></figure>
<h6 id="3-表示原样输出不转义-号转义"><a href="#3-表示原样输出不转义-号转义" class="headerlink" title="3. - 表示原样输出不转义 =号转义"></a>3. - 表示原样输出不转义 =号转义</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">%</span></span></span><br><span class="line"><span class="tag">    <span class="attr">var</span> <span class="attr">str</span>=<span class="string">"&lt;div&gt;&lt;/div&gt;"</span>;</span></span><br><span class="line"><span class="tag">    %&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">%-</span> <span class="attr">str</span> %&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">%=</span> <span class="attr">str</span> %&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h6 id="4-include"><a href="#4-include" class="headerlink" title="4. include"></a>4. include</h6><figure class="highlight erb"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span></span></span><span class="ruby"> <span class="keyword">if</span>(type==<span class="string">'admin'</span>)&#123; </span><span class="xml"><span class="tag">%&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span></span></span><span class="ruby"> <span class="keyword">include</span> ../style/admin.css </span><span class="xml"><span class="tag">%&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span></span></span><span class="ruby">&#125;<span class="keyword">else</span>&#123;</span><span class="xml"><span class="tag">%&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span></span></span><span class="ruby"> <span class="keyword">include</span> ../style/user.css </span><span class="xml"><span class="tag">%&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">%</span></span></span><span class="ruby"> &#125; </span><span class="xml"><span class="tag">%&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_13jade一些语法/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_13jade一些语法/" itemprop="url">13jade一些语法</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:13+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_13jade一些语法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_13jade一些语法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="十三、jade一些语法"><a href="#十三、jade一些语法" class="headerlink" title="十三、jade一些语法"></a>十三、jade一些语法</h3><h6 id="1-原样输出-加"><a href="#1-原样输出-加" class="headerlink" title="1. 原样输出 加 |"></a>1. 原样输出 加 |</h6><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">    script</span><br><span class="line">      |<span class="built_in">window</span>.onload=<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">      | <span class="keyword">var</span> oBtn=<span class="built_in">document</span>.getElementById(<span class="string">'btn1'</span>);</span><br><span class="line">      | oBtn.onclick=<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">      |   alert(<span class="string">'aaaa'</span>);</span><br><span class="line">      | &#125;;</span><br><span class="line">      |&#125;;</span><br><span class="line">  body</span><br><span class="line">    |abc</span><br><span class="line">    |ddd</span><br><span class="line">    |<span class="number">213</span></span><br></pre></td></tr></table></figure>
<h6 id="2-原样输出-加"><a href="#2-原样输出-加" class="headerlink" title="2. 原样输出 加 ."></a>2. 原样输出 加 .</h6><p>表示下面的所有层级都原样输出  eg head.<br><figure class="highlight 1c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head.</span><br><span class="line">    div</span><br><span class="line">  body</span><br><span class="line">    <span class="string">|abc</span></span><br><span class="line">    <span class="string">|ddd</span></span><br><span class="line">    <span class="string">|213</span></span><br></pre></td></tr></table></figure></p>
<h6 id="3-include-加载外部文件"><a href="#3-include-加载外部文件" class="headerlink" title="3.include 加载外部文件"></a>3.include 加载外部文件</h6><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">    script</span><br><span class="line">      include a.js</span><br><span class="line">  body</span><br><span class="line">    |abc</span><br><span class="line">    |ddd</span><br><span class="line">    |213</span><br></pre></td></tr></table></figure>
<h6 id="4-name-传参"><a href="#4-name-传参" class="headerlink" title="4.#{name} 传参"></a>4.#{name} 传参</h6><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">  body</span><br><span class="line">    <span class="selector-tag">div</span> 我的名字：#&#123;a+b&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const jade=require(<span class="string">'jade'</span>);</span><br><span class="line"></span><br><span class="line">console.log(jade.renderFile(<span class="string">'./views/7.jade'</span>, &#123;<span class="string">pretty:</span> <span class="literal">true</span>, <span class="string">a:</span> <span class="number">12</span>, <span class="string">b:</span> <span class="number">5</span>&#125;));</span><br></pre></td></tr></table></figure>
<h6 id="5-直接传-style-json格式-class-数组格式"><a href="#5-直接传-style-json格式-class-数组格式" class="headerlink" title="5.直接传 style json格式 class 数组格式"></a>5.直接传 style json格式 class 数组格式</h6><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">  body</span><br><span class="line">    div(<span class="attribute">style</span>=json)</span><br><span class="line">    div(<span class="attribute">class</span>=arr)</span><br><span class="line">    div(<span class="attribute">class</span>=arr <span class="attribute">class</span>=<span class="string">"active"</span>)</span><br><span class="line">    div(<span class="attribute">class</span>=arr)</span><br></pre></td></tr></table></figure>
<h6 id="6-前面加-表示代码块"><a href="#6-前面加-表示代码块" class="headerlink" title="6. 前面加 - 表示代码块"></a>6. 前面加 - 表示代码块</h6><figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">  <span class="keyword">body</span></span><br><span class="line"><span class="keyword"> </span>   -var a=<span class="number">12</span><span class="comment">;</span></span><br><span class="line">    -var <span class="keyword">b=5;</span></span><br><span class="line"><span class="keyword"> </span>   <span class="keyword">div </span>结果是：<span class="comment">#&#123;a+b&#125;</span></span><br></pre></td></tr></table></figure>
<h6 id="7-for循环"><a href="#7-for循环" class="headerlink" title="7. for循环"></a>7. for循环</h6><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">  body</span><br><span class="line">    -<span class="keyword">for</span>(var <span class="built_in">i</span>=<span class="number">0</span>;<span class="built_in">i</span>&lt;arr.<span class="built_in">length</span>;<span class="built_in">i</span>++)</span><br><span class="line">      div=arr[<span class="built_in">i</span>]</span><br></pre></td></tr></table></figure>
<h6 id="8-标签不转义-加-号"><a href="#8-标签不转义-加-号" class="headerlink" title="8. 标签不转义 加! 号"></a>8. 标签不转义 加! 号</h6><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">body</span><br><span class="line">  div!=<span class="attribute">content</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> jade=<span class="built_in">require</span>(<span class="string">'jade'</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(jade.renderFile(<span class="string">'./views/12.jade'</span>, &#123;<span class="attr">pretty</span>: <span class="literal">true</span>,</span><br><span class="line">  content: <span class="string">"&lt;h2&gt;你好啊&lt;/h2&gt;&lt;p&gt;我是中国人&lt;/p&gt;"</span></span><br><span class="line">&#125;));</span><br></pre></td></tr></table></figure>
<h6 id="9-if-和-else"><a href="#9-if-和-else" class="headerlink" title="9. if 和 else"></a>9. if 和 else</h6><figure class="highlight cal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">body</span><br><span class="line">  -<span class="keyword">var</span> a=<span class="number">19</span>;</span><br><span class="line">  <span class="keyword">if</span>(a%<span class="number">2</span>==<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">div</span>(style=&#123;background:<span class="string">'red'</span>&#125;) 偶数</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="keyword">div</span>(style=&#123;background:<span class="string">'green'</span>&#125;) 奇数</span><br></pre></td></tr></table></figure>
<h6 id="10-case-when-相当于-switch-case"><a href="#10-case-when-相当于-switch-case" class="headerlink" title="10. case when  相当于 switch case"></a>10. case when  相当于 switch case</h6><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">  body</span><br><span class="line">    -var <span class="attribute">a</span>=1;</span><br><span class="line">    case a</span><br><span class="line">      when 0</span><br><span class="line">        div aaa</span><br><span class="line">      when 1</span><br><span class="line">        div bbb</span><br><span class="line">      default</span><br><span class="line">        |不靠谱</span><br></pre></td></tr></table></figure>
<h6 id="11-while-循环"><a href="#11-while-循环" class="headerlink" title="11. while 循环"></a>11. while 循环</h6><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">body</span><br><span class="line">  -var <span class="attribute">a</span>=0;</span><br><span class="line">  <span class="keyword">while</span> a&lt;12</span><br><span class="line">    <span class="keyword">if</span> a%<span class="attribute">4</span>==0 &amp;&amp; a!=0</span><br><span class="line">      div.<span class="attribute">last</span>=a++</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">      <span class="attribute">div</span>=a++</span><br></pre></td></tr></table></figure>
<h4 id="小示例"><a href="#小示例" class="headerlink" title="小示例"></a>小示例</h4><p>//index.jade<br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">doctype</span><br><span class="line">html</span><br><span class="line">  head</span><br><span class="line">    meta(charset=<span class="string">"utf-8"</span>)</span><br><span class="line">    <span class="built_in">title</span> jade测试页面</span><br><span class="line">    <span class="built_in">style</span>.</span><br><span class="line">      div &#123;<span class="built_in">width</span>:100px;<span class="built_in">height</span>:100px;<span class="built_in">background</span>:#CCC;text-align:<span class="built_in">center</span>;line-<span class="built_in">height</span>:100px;<span class="built_in">float</span>:left;margin:10px auto&#125;</span><br><span class="line">      div.<span class="built_in">last</span> &#123;clear:left&#125;</span><br><span class="line">  body</span><br><span class="line">    -<span class="built_in">var</span> a=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> a&lt;<span class="number">12</span></span><br><span class="line">      <span class="keyword">if</span> a%<span class="number">4</span>==<span class="number">0</span> &amp;&amp; a!=<span class="number">0</span></span><br><span class="line">        div.<span class="built_in">last</span>=a++</span><br><span class="line">      <span class="keyword">else</span></span><br><span class="line">        div=a++</span><br></pre></td></tr></table></figure></p>
<p>//index.js<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> jade=<span class="built_in">require</span>(<span class="string">'jade'</span>);</span><br><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> str=jade.renderFile(<span class="string">'./views/index.jade'</span>, &#123;<span class="attr">pretty</span>: <span class="literal">true</span>&#125;);</span><br><span class="line"></span><br><span class="line">fs.writeFile(<span class="string">'./build/index.html'</span>, str, <span class="function"><span class="keyword">function</span> (<span class="params">err</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'编译失败'</span>);</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'成功'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_12模板引擎jade/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_12模板引擎jade/" itemprop="url">12模板引擎jade</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:12+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_12模板引擎jade/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_12模板引擎jade/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="十二、模板引擎jade"><a href="#十二、模板引擎jade" class="headerlink" title="十二、模板引擎jade"></a>十二、模板引擎jade</h3><figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="keyword">install </span><span class="keyword">jade</span></span><br><span class="line"><span class="keyword">npm </span><span class="keyword">install </span>ejs</span><br></pre></td></tr></table></figure>
<h6 id="jade-破坏式、侵入式、强依赖"><a href="#jade-破坏式、侵入式、强依赖" class="headerlink" title="jade-破坏式、侵入式、强依赖"></a>jade-破坏式、侵入式、强依赖</h6><ol>
<li>根据缩进，规定层级</li>
<li>属性放在()里面，逗号分隔</li>
<li>内容空个格，直接往后堆<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">html				<span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">	head				<span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">		style				<span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">	body				<span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">		div				<span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		div				<span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><strong>属性</strong><br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">script</span><span class="params">(src=<span class="string">"a.js"</span>)</span></span> <span class="comment">//jade写法</span></span><br><span class="line">&lt;script src=<span class="string">"a.js"</span>&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>内容</strong><br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">a</span><span class="params">(href=<span class="string">"http://www.zhinengshe.com/"</span>)</span></span> 官网</span><br><span class="line">&lt;<span class="selector-tag">a</span> href=<span class="string">"http://www.zhinengshe.com/"</span>&gt;官网&lt;/a&gt;</span><br></pre></td></tr></table></figure></p>
<h5 id="style-”width-200px-height-200px-background-red-”"><a href="#style-”width-200px-height-200px-background-red-”" class="headerlink" title="style=”width:200px;height:200px;background:red;”"></a>style=”width:200px;height:200px;background:red;”</h5><ol>
<li><p>普通属性写法</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">div</span><span class="params">(style=<span class="string">"width:200px;height:200px;background:red"</span>)</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>用json</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">div(style= &#123;<span class="built_in">width</span>: <span class="string">'200px'</span>, <span class="built_in">height</span>: <span class="string">'200px'</span>, <span class="built_in">background</span>: <span class="string">'red'</span>&#125;)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h5 id="class-”aaa-left-swrap-active”"><a href="#class-”aaa-left-swrap-active”" class="headerlink" title="class=”aaa left-swrap active”"></a>class=”aaa left-swrap active”</h5><ol>
<li><p>普通属性写法</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">div</span><span class="params">(class=<span class="string">"aaa left-warp active"</span>)</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>用arr</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">div</span><span class="params">(class= [<span class="string">'aaa'</span>, <span class="string">'left-warp'</span>, <span class="string">'active'</span>])</span></span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h6 id="a-jade-render-‘字符串’"><a href="#a-jade-render-‘字符串’" class="headerlink" title="a. jade.render(‘字符串’);"></a>a. jade.render(‘字符串’);</h6><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> jade=<span class="built_in">require</span>(<span class="string">'jade'</span>);</span><br><span class="line"><span class="keyword">var</span> str=jade.render(<span class="string">'html'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(str);</span><br></pre></td></tr></table></figure>
<h6 id="b-jade-renderFile-‘模板文件名’-参数"><a href="#b-jade-renderFile-‘模板文件名’-参数" class="headerlink" title="b. jade.renderFile(‘模板文件名’, 参数)"></a>b. jade.renderFile(‘模板文件名’, 参数)</h6><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> jade=<span class="built_in">require</span>(<span class="string">'jade'</span>);</span><br><span class="line"><span class="keyword">var</span> str=jade.renderFile(<span class="string">'./views/8.jade'</span>, &#123;<span class="attr">pretty</span>: <span class="literal">true</span>&#125;);</span><br><span class="line"><span class="comment">//var str=jade.renderFile('./views/8.jade');//pretty 排版美化</span></span><br><span class="line"><span class="built_in">console</span>.log(str);</span><br></pre></td></tr></table></figure>
<hr>
<h6 id="ejs-温和、非侵入式、弱依赖"><a href="#ejs-温和、非侵入式、弱依赖" class="headerlink" title="ejs-温和、非侵入式、弱依赖"></a>ejs-温和、非侵入式、弱依赖</h6><p>&lt;%= name %&gt;<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ejs=<span class="built_in">require</span>(<span class="string">'ejs'</span>);</span><br><span class="line"></span><br><span class="line">ejs.renderFile(<span class="string">'./views/1.ejs'</span>, &#123;<span class="attr">name</span>: <span class="string">'blue'</span>&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">err, data</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'编译失败'</span>);</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">console</span>.log(data);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//1.ejs</span><br><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    我的名字叫：<span class="tag">&lt;<span class="name">%=</span> <span class="attr">name</span> %&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_11cookie和session/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_11cookie和session/" itemprop="url">11cookie和session</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:11+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_11cookie和session/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_11cookie和session/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="十一、cookie和session"><a href="#十一、cookie和session" class="headerlink" title="十一、cookie和session"></a>十一、cookie和session</h3><p>http-无状态的,要保存用户数据所以就有了cookie和session</p>
<ol>
<li><p>cookie：在浏览器保存一些数据，每次请求都会带过来<br>*不安全、有限(4K)</p>
</li>
<li><p>session：保存数据，保存在服务端<br>*安全、无限</p>
</li>
</ol>
<hr>
<p>session：基于cookie实现的<br>  *cookie中会有一个session的ID，服务器利用sessionid找到session文件、读取、写入</p>
<p>  隐患：session劫持   </p>
<hr>
<h4 id="cookie"><a href="#cookie" class="headerlink" title="cookie"></a>cookie</h4><blockquote>
<ol>
<li>cookie空间非常小—省着用,精打细算</li>
<li>安全性非常差,校验cookie是否被篡改过</li>
</ol>
</blockquote>
<h5 id="a-发送cookie"><a href="#a-发送cookie" class="headerlink" title="a.发送cookie"></a>a.发送cookie</h5><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">res.secret=<span class="string">'字符串'</span>;<span class="comment">//加签名   这个可不写 server.use(cookieParser('签名字符串'));时已经加了</span></span><br><span class="line">res.cookie(名字, 值, &#123;<span class="string">path:</span> <span class="string">'/'</span>, <span class="string">maxAge:</span> 毫秒, <span class="string">signed:</span> <span class="literal">true</span>&#125;);</span><br><span class="line"><span class="comment">//signed 是否加签名</span></span><br><span class="line"><span class="comment">//maxAge 有效期以毫秒为单位</span></span><br><span class="line"><span class="comment">//eg </span></span><br><span class="line">res.cookie(<span class="string">'user'</span>, <span class="string">'blue'</span>, &#123;<span class="string">path:</span> <span class="string">'/aaa'</span>, <span class="string">maxAge:</span> <span class="number">30</span>*<span class="number">24</span>*<span class="number">3600</span>*<span class="number">1000</span>&#125;);</span><br></pre></td></tr></table></figure>
<h5 id="b-读取cookie"><a href="#b-读取cookie" class="headerlink" title="b.读取cookie"></a>b.读取cookie</h5><p>cookie-parser<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> cookieParser=<span class="keyword">require</span>(<span class="string">'cookie-parser'</span>);<span class="comment">//加载</span></span><br><span class="line">server.<span class="keyword">use</span>(cookieParser());<span class="comment">//使用</span></span><br><span class="line"></span><br><span class="line">server.<span class="keyword">use</span>(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span></span>&#123;</span><br><span class="line">  console.log(req.cookies);<span class="comment">//cookies</span></span><br><span class="line">  res.send(<span class="string">'ok'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">server.use(cookieParser(<span class="string">'签名字符串'</span>));</span><br><span class="line"><span class="comment">//发送时 signed: true有签名</span></span><br><span class="line">server.use(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'签名cookie：'</span>, req.signedCookies)</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'无签名cookie：'</span>, req.cookies);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h5 id="c-删除cookie"><a href="#c-删除cookie" class="headerlink" title="c.删除cookie"></a>c.删除cookie</h5><figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">res.clearCookie(名字)<span class="comment">;</span></span><br></pre></td></tr></table></figure>
<hr>
<h4 id="session"><a href="#session" class="headerlink" title="session"></a>session</h4><p>cookie-session<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> cookieSession=<span class="keyword">require</span>(<span class="string">'cookie-session'</span>);<span class="comment">//加载</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">    server.use(cookieSession(&#123;</span></span><br><span class="line"><span class="comment">	keys: [.., .., .., ..]</span></span><br><span class="line"><span class="comment">&#125;));</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">server.<span class="keyword">use</span>(cookieParser());</span><br><span class="line">server.<span class="keyword">use</span>(cookieSession(&#123;</span><br><span class="line">  name: <span class="string">'sess'</span>,</span><br><span class="line">  keys: [<span class="string">'aaa'</span>, <span class="string">'bbb'</span>, <span class="string">'ccc'</span>],</span><br><span class="line">  maxAge: <span class="number">2</span>*<span class="number">3600</span>*<span class="number">1000</span></span><br><span class="line">&#125;));</span><br><span class="line"></span><br><span class="line">server.<span class="keyword">use</span>(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span></span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(req.session[<span class="string">'count'</span>]==<span class="keyword">null</span>)&#123;</span><br><span class="line">    req.session[<span class="string">'count'</span>]=<span class="number">1</span>;</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    req.session[<span class="string">'count'</span>]++;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  console.log(req.session[<span class="string">'count'</span>]);</span><br><span class="line"></span><br><span class="line">  res.send(<span class="string">'ok'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<h5 id="删除session"><a href="#删除session" class="headerlink" title="删除session"></a>删除session</h5><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">delete</span> <span class="selector-tag">req</span><span class="selector-class">.session</span></span><br><span class="line"></span><br><span class="line">	<span class="selector-tag">res</span><span class="selector-class">.session</span><span class="selector-attr">['xxx']</span></span><br><span class="line">	<span class="selector-tag">delete</span> <span class="selector-tag">res</span><span class="selector-class">.session</span><span class="selector-attr">['xxx']</span>;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_10express了解、中间件、链式操作/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_10express了解、中间件、链式操作/" itemprop="url">10express了解、中间件、链式操作</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:10+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_10express了解、中间件、链式操作/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_10express了解、中间件、链式操作/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="十、express-了解"><a href="#十、express-了解" class="headerlink" title="十、express 了解"></a>十、express 了解</h3><h4 id="Express："><a href="#Express：" class="headerlink" title="Express："></a>Express：</h4><h5 id="1-数据：GET、POST"><a href="#1-数据：GET、POST" class="headerlink" title="1. 数据：GET、POST"></a>1. 数据：GET、POST</h5><h6 id="GET-无需中间件"><a href="#GET-无需中间件" class="headerlink" title="GET-无需中间件"></a>GET-无需中间件</h6><p>req.query<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">server.use(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(req.query); <span class="comment">//GET</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<h6 id="POST-需要”body-parser”"><a href="#POST-需要”body-parser”" class="headerlink" title="POST-需要”body-parser”"></a>POST-需要”body-parser”</h6><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express=<span class="keyword">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> bodyParser=<span class="keyword">require</span>(<span class="string">'body-parser'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> server=express();</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"><span class="comment">//中间件body-parser 加工post数据然后放到 req.body里</span></span><br><span class="line"><span class="comment">//server.use(bodyParser.urlencoded(&#123;&#125;));//不带参数</span></span><br><span class="line">server.<span class="keyword">use</span>(bodyParser.urlencoded(&#123;</span><br><span class="line">  extended: <span class="keyword">false</span>,                 <span class="comment">//扩展模式</span></span><br><span class="line">  limit:    <span class="number">2</span>*<span class="number">1024</span>*<span class="number">1024</span>           <span class="comment">//限制-2M</span></span><br><span class="line">&#125;));</span><br><span class="line"></span><br><span class="line">server.<span class="keyword">use</span>(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span></span>&#123;</span><br><span class="line">  console.log(req.body); <span class="comment">//POST</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h5 id="2-中间件：使用、写、链式操作"><a href="#2-中间件：使用、写、链式操作" class="headerlink" title="2. 中间件：使用、写、链式操作"></a>2. 中间件：使用、写、链式操作</h5><h6 id="链式操作："><a href="#链式操作：" class="headerlink" title="链式操作："></a>链式操作：</h6><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">server.use(<span class="function"><span class="keyword">function</span> <span class="params">(req, res, next)</span></span>&#123;&#125;);</span><br><span class="line">server.get(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res, next)</span></span>&#123;&#125;);</span><br><span class="line">server.post(<span class="function"><span class="keyword">function</span> <span class="params">(req, res, next)</span></span>&#123;&#125;);</span><br><span class="line"></span><br><span class="line">// <span class="built_in">next</span>——下一个步骤</span><br><span class="line"><span class="built_in">next</span>();</span><br><span class="line"></span><br><span class="line">server.use(<span class="string">'/login'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span></span>&#123;</span><br><span class="line">  mysql.query(<span class="function"><span class="keyword">function</span> <span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(有错)</span><br><span class="line">      res.emit(<span class="string">'error'</span>);</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">      <span class="built_in">next</span>();</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h6 id="中间件-body-parser-、自己写中间件"><a href="#中间件-body-parser-、自己写中间件" class="headerlink" title="中间件(body-parser)、自己写中间件"></a>中间件(body-parser)、自己写中间件</h6><p>body-parser<br><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">  server.<span class="keyword">use</span>(bodyParser.urlencoded(&#123;&#125;));<span class="comment">//不带参数</span></span><br><span class="line">  server.<span class="keyword">use</span>('/', <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span>&#123;</span></span><br><span class="line">    console.<span class="built_in">log</span>(req.body); <span class="comment">//POST</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<p>自己写的中间件my-body-parser.js<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> querystring=<span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports=<span class="function"><span class="keyword">function</span> (<span class="params">req, res, next</span>)</span>&#123;</span><br><span class="line">      <span class="keyword">var</span> str=<span class="string">''</span>;</span><br><span class="line">      req.on(<span class="string">'data'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">data</span>)</span>&#123;</span><br><span class="line">        str+=data;</span><br><span class="line">      &#125;);</span><br><span class="line">      req.on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        req.body=querystring.parse(str);</span><br><span class="line"></span><br><span class="line">        next();</span><br><span class="line">      &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>使用<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> bodyParser2=<span class="keyword">require</span>(<span class="string">'./libs/my-body-parser'</span>);</span><br><span class="line">server.<span class="keyword">use</span>(bodyParser2);</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_09express框架/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_09express框架/" itemprop="url">09express框架</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:09+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_09express框架/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_09express框架/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="1-安装"><a href="#1-安装" class="headerlink" title="1.安装"></a>1.安装</h3><figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="keyword">install</span> express</span><br></pre></td></tr></table></figure>
<h3 id="2-配置"><a href="#2-配置" class="headerlink" title="2.配置"></a>2.配置</h3><h3 id="3-接收请求"><a href="#3-接收请求" class="headerlink" title="3.接收请求"></a>3.接收请求</h3><h3 id="4-响应"><a href="#4-响应" class="headerlink" title="4.响应"></a>4.响应</h3><hr>
<p>express保留了原生的功能，添加了一些方法(send)，增强原有的功能</p>
<h6 id="三个步骤"><a href="#三个步骤" class="headerlink" title="三个步骤"></a>三个步骤</h6><ol>
<li><p>创建服务   </p>
<figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">var server</span>=express();</span><br></pre></td></tr></table></figure>
</li>
<li><p>监听  </p>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">server.<span class="section">listen</span>(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>处理请求</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server.<span class="keyword">use</span>(<span class="string">'地址'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span></span>&#123;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
</li>
</ol>
<hr>
<h6 id="三种方法："><a href="#三种方法：" class="headerlink" title="三种方法："></a>三种方法：</h6><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.get(<span class="string">'/'</span>, <span class="keyword">function</span> <span class="title"></span>(req, res)&#123;&#125;);</span><br><span class="line">.post(<span class="string">'/'</span>, <span class="keyword">function</span> <span class="title"></span>(req, res)&#123;&#125;);</span><br><span class="line">.<span class="keyword">use</span>(<span class="string">'/'</span>, <span class="keyword">function</span> <span class="title"></span>(req, res)&#123;&#125;);</span><br></pre></td></tr></table></figure>
<p>use 通吃，get和post都响应   </p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">const express=<span class="built_in">require</span>(<span class="string">'express'</span>);</span><br><span class="line">var server=express();</span><br><span class="line">/*</span><br><span class="line">server.get(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span></span>&#123;</span><br><span class="line">  console.<span class="built_in">log</span>(<span class="string">'有GET'</span>);</span><br><span class="line">&#125;);</span><br><span class="line">server.post(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span></span>&#123;</span><br><span class="line">  console.<span class="built_in">log</span>(<span class="string">'有POST'</span>);</span><br><span class="line">&#125;);</span><br><span class="line">*/</span><br><span class="line">server.use(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> <span class="params">()</span></span>&#123;</span><br><span class="line">  console.<span class="built_in">log</span>(<span class="string">'use了'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure>
<h5 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h5><p>express框架：</p>
<ol>
<li>依赖中间件</li>
<li>接收请求<br>get/post/use<br>get(‘/地址’, function (req, res){});</li>
<li>非破坏式的，原生的还可用，增加了一些方法。<br>req.url</li>
<li>static用法<br>const static=require(‘express-static’);<br>server.use(static(‘./www’));</li>
</ol>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express=<span class="keyword">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> expressStatic=<span class="keyword">require</span>(<span class="string">'express-static'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> server=express();</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//用户数据</span></span><br><span class="line"><span class="keyword">var</span> users=&#123;</span><br><span class="line">  <span class="string">'blue'</span>: <span class="string">'123456'</span>,</span><br><span class="line">  <span class="string">'zhangsan'</span>: <span class="string">'654321'</span>,</span><br><span class="line">  <span class="string">'lisi'</span>: <span class="string">'987987'</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">server.get(<span class="string">'/login'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(req, res)</span></span>&#123;</span><br><span class="line">  <span class="keyword">var</span> user=req.query[<span class="string">'user'</span>];</span><br><span class="line">  <span class="keyword">var</span> pass=req.query[<span class="string">'pass'</span>];</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(users[user]==<span class="keyword">null</span>)&#123;</span><br><span class="line">    res.send(&#123;ok: <span class="keyword">false</span>, msg: <span class="string">'此用户不存在'</span>&#125;);</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(users[user]!=pass)&#123;</span><br><span class="line">      res.send(&#123;ok: <span class="keyword">false</span>, msg: <span class="string">'密码错了'</span>&#125;);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      res.send(&#123;ok: <span class="keyword">true</span>, msg: <span class="string">'成功'</span>&#125;);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">server.<span class="keyword">use</span>(expressStatic(<span class="string">'./www'</span>));</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_08自定义模块/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_08自定义模块/" itemprop="url">08自定义模块</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:08+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_08自定义模块/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_08自定义模块/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h5 id="八-自定义模块"><a href="#八-自定义模块" class="headerlink" title="八.自定义模块"></a>八.自定义模块</h5><figure class="highlight livescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A<span class="function"><span class="params">(模块化)</span>--&gt;</span>B(系统模块)</span><br><span class="line">A-<span class="function">-&gt;</span>C(自定义模块)</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="1-模块组成"><a href="#1-模块组成" class="headerlink" title="1. 模块组成"></a>1. 模块组成</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">require     <span class="comment">//引入其他模块</span></span><br><span class="line"><span class="keyword">exports</span>     <span class="comment">//一个一个输出</span></span><br><span class="line"><span class="keyword">module</span>      <span class="comment">//批量输出</span></span><br></pre></td></tr></table></figure>
<h3 id="2-npm"><a href="#2-npm" class="headerlink" title="2. npm"></a>2. npm</h3><h5 id="npm：NodeJS-Package-Manager-NodeJS包管理器"><a href="#npm：NodeJS-Package-Manager-NodeJS包管理器" class="headerlink" title="npm：NodeJS Package Manager(NodeJS包管理器)"></a>npm：NodeJS Package Manager(NodeJS包管理器)</h5><ol>
<li>统一下载途径 node_modules</li>
<li>自动下载依赖</li>
</ol>
<h3 id="3-发布自己的模块"><a href="#3-发布自己的模块" class="headerlink" title="3. 发布自己的模块"></a>3. 发布自己的模块</h3><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm init        <span class="string">//</span>初始化</span><br><span class="line">npm publish     <span class="string">//</span>发布包</span><br><span class="line">npm <span class="params">--force</span> unpublish <span class="string">//</span>卸载包</span><br></pre></td></tr></table></figure>
<h3 id="npm官网"><a href="#npm官网" class="headerlink" title="npm官网"></a><a href="https://www.npmjs.com/" target="_blank" rel="noopener">npm官网</a></h3><ol>
<li><p>自己的模块</p>
<figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">require</span></span><br><span class="line"><span class="class"><span class="keyword">module</span></span></span><br><span class="line">exports</span><br></pre></td></tr></table></figure>
</li>
<li><figure class="highlight plain"><figcaption><span>引入模块加	./	   </span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">    1.如果有&quot;./&quot;   </span><br><span class="line">    	从当前目录找   </span><br><span class="line"></span><br><span class="line">    2.如果没有&quot;./&quot;   </span><br><span class="line">    	先从系统模块   </span><br><span class="line">	    再从node_modules找   </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">3. &quot;.js&quot;可选   </span><br><span class="line">eg:  ```const mod1=require(&apos;./mod&apos;);```</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>npm install xxx<br>npm uninstall xxx<br><code>`</code></p>
<h2 id="小结"><a href="#小结" class="headerlink" title="小结   "></a><strong>小结</strong>   </h2><ol>
<li><p>模块里面<br>1.1 require——引入<br>1.2 exports——输出<br>1.3 module.exports——批量输出</p>
</li>
<li><p>npm<br>2.1 帮咱们下载模块<br>2.2 自动解决依赖   </p>
</li>
<li><p>node_modules    模块放这里</p>
</li>
</ol>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_07nodejs系统模块/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_07nodejs系统模块/" itemprop="url">07nodejs系统模块</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:07+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_07nodejs系统模块/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_07nodejs系统模块/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="nodejs官网docs-API"><a href="#nodejs官网docs-API" class="headerlink" title="nodejs官网docs API"></a><a href="https://nodejs.org/en/docs/" target="_blank" rel="noopener">nodejs官网docs API</a></h3><p>Stability 稳定性</p>
<h5 id="系统模块："><a href="#系统模块：" class="headerlink" title="系统模块："></a>系统模块：</h5><p>Crypto    加密<br>Events    事件<br>Net    网络操作<br>OS    操作系统信息<br>Path    处理文件路径<br>Stream    流操作<br>Timers    定时器<br>ZLIB    压缩   </p>
<p>require</p>
<hr>
<p><strong>time.js</strong><br><figure class="highlight lisp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">setInterval(<span class="name">function</span> ()&#123;</span><br><span class="line">  console.log(<span class="name">Math</span>.random())<span class="comment">;</span></span><br><span class="line">&#125;, <span class="number">100</span>)<span class="comment">;</span></span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_06注册登录小示例/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_06注册登录小示例/" itemprop="url">06注册登录小示例</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:06+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_06注册登录小示例/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_06注册登录小示例/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h5 id="六-注册登录小示例"><a href="#六-注册登录小示例" class="headerlink" title="六.注册登录小示例"></a>六.注册登录小示例</h5><p>1.接口：<br>注册： /user?act=reg&amp;user=aaa&amp;pass=123456<br>返回JSON {“ok”: false, “msg”: “原因”}   </p>
<p>登录: /user?act=login&amp;user=aaa&amp;pass=123456   </p>
<h2 id="返回JSON-“ok”-true-“msg”-“原因”"><a href="#返回JSON-“ok”-true-“msg”-“原因”" class="headerlink" title="返回JSON {“ok”: true, “msg”: “原因”}"></a>返回JSON {“ok”: true, “msg”: “原因”}</h2><h6 id="1-分两部分"><a href="#1-分两部分" class="headerlink" title="1.分两部分"></a>1.分两部分</h6><p><strong>1.1对文件访问：</strong><br><a href="http://localhost:8080/1.html" target="_blank" rel="noopener">http://localhost:8080/1.html</a><br><a href="http://localhost:8080/ajax.js" target="_blank" rel="noopener">http://localhost:8080/ajax.js</a><br><a href="http://localhost:8080/1.jpg" target="_blank" rel="noopener">http://localhost:8080/1.jpg</a>   </p>
<p><strong>1.2对接口访问：</strong><br><a href="http://localhost:8080/user?act=xx.." target="_blank" rel="noopener">http://localhost:8080/user?act=xx..</a>.<br><a href="http://localhost:8080/news?act=xxx.." target="_blank" rel="noopener">http://localhost:8080/news?act=xxx..</a>.   </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http=<span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">const</span> querystring=<span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"><span class="keyword">const</span> urlLib=<span class="built_in">require</span>(<span class="string">'url'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> users=&#123;&#125;;   <span class="comment">//&#123;"blue": "123456", "zhangsan": "123456", "lisi": "321321"&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> server=http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>)</span>&#123;</span><br><span class="line">  <span class="comment">//解析数据</span></span><br><span class="line">  <span class="keyword">var</span> str=<span class="string">''</span>;</span><br><span class="line">  req.on(<span class="string">'data'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">data</span>)</span>&#123;</span><br><span class="line">    str+=data;</span><br><span class="line">  &#125;);</span><br><span class="line">  req.on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> obj=urlLib.parse(req.url, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> url=obj.pathname;</span><br><span class="line">    <span class="keyword">const</span> GET=obj.query;</span><br><span class="line">    <span class="keyword">const</span> POST=querystring.parse(str);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//区分——接口、文件</span></span><br><span class="line">    <span class="keyword">if</span>(url==<span class="string">'/user'</span>)&#123;   <span class="comment">//接口</span></span><br><span class="line">      <span class="keyword">switch</span>(GET.act)&#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'reg'</span>:</span><br><span class="line">          <span class="comment">//1.检查用户名是否已经有了</span></span><br><span class="line">          <span class="keyword">if</span>(users[GET.user])&#123;</span><br><span class="line">            res.write(<span class="string">'&#123;"ok": false, "msg": "此用户已存在"&#125;'</span>);</span><br><span class="line">          &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//2.插入users</span></span><br><span class="line">            users[GET.user]=GET.pass;</span><br><span class="line">            res.write(<span class="string">'&#123;"ok": true, "msg": "注册成功"&#125;'</span>);</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">'login'</span>:</span><br><span class="line">          <span class="comment">//1.检查用户是否存在</span></span><br><span class="line">          <span class="keyword">if</span>(users[GET.user]==<span class="literal">null</span>)&#123;</span><br><span class="line">            res.write(<span class="string">'&#123;"ok": false, "msg": "此用户不存在"&#125;'</span>);</span><br><span class="line">          <span class="comment">//2.检查用户密码</span></span><br><span class="line">          &#125;<span class="keyword">else</span> <span class="keyword">if</span>(users[GET.user]!=GET.pass)&#123;</span><br><span class="line">            res.write(<span class="string">'&#123;"ok": false, "msg": "用户名或密码有误"&#125;'</span>);</span><br><span class="line">          &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            res.write(<span class="string">'&#123;"ok": true, "msg": "登录成功"&#125;'</span>);</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">          res.write(<span class="string">'&#123;"ok": false, "msg": "未知的act"&#125;'</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      res.end();</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;              <span class="comment">//文件</span></span><br><span class="line">      <span class="comment">//读取文件</span></span><br><span class="line">      <span class="keyword">var</span> file_name=<span class="string">'./www'</span>+url;</span><br><span class="line">      fs.readFile(file_name, <span class="function"><span class="keyword">function</span> (<span class="params">err, data</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(err)&#123;</span><br><span class="line">          res.write(<span class="string">'404'</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">          res.write(data);</span><br><span class="line">        &#125;</span><br><span class="line">        res.end();</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/08/nodejs_05数据请求---post/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑诗增">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学习笔记">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/06/08/nodejs_05数据请求---post/" itemprop="url">05数据请求---post</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-08T06:26:05+08:00">
                2019-06-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/nodejs/" itemprop="url" rel="index">
                    <span itemprop="name">nodejs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/08/nodejs_05数据请求---post/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/06/08/nodejs_05数据请求---post/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h5 id="五、数据请求-—post"><a href="#五、数据请求-—post" class="headerlink" title="五、数据请求 —post"></a>五、数据请求 —post</h5><h6 id="1-POST数据接收：POST数据比GET大得多"><a href="#1-POST数据接收：POST数据比GET大得多" class="headerlink" title="1.POST数据接收：POST数据比GET大得多"></a>1.POST数据接收：POST数据比GET大得多</h6><p>POST很大——分段<br>data    一段数据<br>end    全部到达<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http=<span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> querystring=<span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>)</span>&#123;</span><br><span class="line">  <span class="comment">//POST——req</span></span><br><span class="line">  <span class="keyword">var</span> str=<span class="string">''</span>;   <span class="comment">//接收数据</span></span><br><span class="line">  <span class="comment">//data——有一段数据到达(很多次)</span></span><br><span class="line">  <span class="keyword">var</span> i=<span class="number">0</span>;</span><br><span class="line">  req.on(<span class="string">'data'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">data</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`第<span class="subst">$&#123;i++&#125;</span>次收到数据`</span>);</span><br><span class="line">    str+=data;</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="comment">//end——数据全部到达(一次)</span></span><br><span class="line">  req.on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> POST=querystring.parse(str);</span><br><span class="line">    <span class="built_in">console</span>.log(POST);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;).listen(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure></p>
<h6 id="2-Get和Post综合、文件请求"><a href="#2-Get和Post综合、文件请求" class="headerlink" title="2.Get和Post综合、文件请求"></a>2.Get和Post综合、文件请求</h6><figure class="highlight qml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http=<span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> fs=<span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="keyword">const</span> querystring=<span class="built_in">require</span>(<span class="string">'querystring'</span>);</span><br><span class="line"><span class="keyword">const</span> urlLib=<span class="built_in">require</span>(<span class="string">'url'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> server=http.createServer(<span class="function"><span class="keyword">function</span> (<span class="params">req, res</span>)</span>&#123;</span><br><span class="line">  <span class="comment">//GET</span></span><br><span class="line">  <span class="keyword">var</span> obj=urlLib.parse(req.url, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> <span class="built_in">url</span>=obj.pathname;</span><br><span class="line">  <span class="keyword">const</span> GET=obj.query;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//POST</span></span><br><span class="line">  <span class="keyword">var</span> str=<span class="string">''</span>;</span><br><span class="line">  req.on(<span class="string">'data'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">data</span>)</span>&#123;</span><br><span class="line">    str+=data;</span><br><span class="line">  &#125;);</span><br><span class="line">  req.on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> POST=querystring.parse(str);</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    url——要什么</span></span><br><span class="line"><span class="comment">    GET——get数据</span></span><br><span class="line"><span class="comment">    POST——post数据</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="comment">//文件请求</span></span><br><span class="line">    <span class="keyword">var</span> file_name=<span class="string">'./www'</span>+<span class="built_in">url</span>;</span><br><span class="line">    fs.readFile(file_name, <span class="function"><span class="keyword">function</span> (<span class="params">err, data</span>)</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(err)&#123;</span><br><span class="line">        res.write(<span class="string">'404'</span>);</span><br><span class="line">      &#125;<span class="title">else</span>&#123;</span><br><span class="line">        res.write(data);</span><br><span class="line">      &#125;</span><br><span class="line">      res.end();</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8081</span>);</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    
    <div>
    
    </div>
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/page/4/">4</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">郑诗增</p>
              <p class="site-description motion-element" itemprop="description">个人学习笔记,好记性不如烂笔头.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">38</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郑诗增</span>

  
</div>
<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>
<span class="post-meta-divider">|</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共10.3k字</span>
</div>

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '8pzNF65sGpQqST6Pm4eBYqrr-gzGzoHsz',
        appKey: 'i9yNJ9zTr4r1c1M9IP2yPvKQ',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

  <!-- 代码块复制功能 -->
  <script type="text/javascript" src="/js/src/clipboard.min.js"></script>  
  <script type="text/javascript" src="/js/src/clipboard-use.js"></script>
</body>
</html>
